(function(window){

	var tracking=function(){
		this.initialize()
	}

	tracking.prototype={

		initialize:function(){
			//this.setDefaultUser()
		},

		setDefaultUser:function(){
			var user=this.parse(this.getDefaultUser())||{},
				id=user.default_id;
			if(!id){
				user.default_id=this.defaultId();
				this.setCookie('default_user',this.join(user),365)
			}
		},

		identifyUser:{},
		setIdentifyUser:function(props){
			var user=this.identifyUser;
			user=this.extend(user,props,true);

			this.identifyUser=user
		},

		setRegistUser:function(props){
			var user=this.parse(this.getRegistUser())||{};
			user=this.extend(user,props,true);

			this.setCookie('regist_user',this.join(user),365)
		},

		getDefaultUser:function(){
			return this.getCookie('default_user')||''
		},

		getRegistUser:function(){
			return this.getCookie('regist_user')||''
		},

		setDefaultInfo:function(props) {
			var id=this.getDefaultUser();
			var user={id:id,props:props};
			//this.localStorage.setObject(this.generatedUserKey,user)
		},

		removeRegistUser:function(){
			this.removeCookie('regist_user')
		},



		track:function(event,value,props){
			var d_user={}, //this.parse(this.getDefaultUser())||{},
				r_user=this.parse(this.getRegistUser())||{},
				i_user=this.identifyUser,
				user=this.extend(this.extend(d_user,r_user,true),i_user,true),
				data=this.extend(user,props,true);
			
			data.fht_tracking_id=this.trackingId;
			data.fht_event=event;
			data.timestamp=new Date().getTime();

			if(this.subDomain){
				data.fht_tracking_group=this.subDomain
			}

			if(value){
				data.fht_event_value=value
			}

			this.send(this.join(data))
		},

		send:function(data){
			var img=document.createElement("img"),
				self=this,
				callback=this.callback;

			img.width = 1;
			img.height = 1;
			img.src='http://121.40.55.191:8098/collect?'+data;
			img.onload=function(){
				callback && callback(self.parse(data))
			};
			img.onerror=function(){
				console.log('fail')
			}
		},

		join:function(obj){
			var result=[];

			for(var i in obj){
				result.push(i+'='+obj[i])
			}

			return result.join('&')
		},

		parse:function(str){
			if(!str){ return {}}

			var str=str.split('&'),
				result={};

			str.forEach(function(i){
				i=i.split('=');
				result[i[0]]=i[1]
			});

			return result
		},

		extend:function(o,n,override){
			for(var p in n)if(n.hasOwnProperty(p) && (!o.hasOwnProperty(p) || override))o[p]=n[p];
			return o
		},

		cookie:{
			get:function(a) {
				for (var a = a + "=", b = document.cookie.split(";"), c = 0; c < b.length; c++) {
					for (var e = b[c];
					" " == e.charAt(0);) e = e.substring(1, e.length);
					if (0 == e.indexOf(a)) return decodeURIComponent(e.substring(a.length, e.length))
				}
				return null
			},
			set:function(a, b, c, e, f) {
				var d;
				var k = "",
					j = "",
					i = "";
				e && (d = (k = (k = document.location.hostname.match(/[a-z0-9][a-z0-9\-]+\.[a-z\.]{2,6}$/i)) ? k[0] : "") ? "; domain=." + k : "", k = d);
				c && (j = new Date, j.setTime(j.getTime() + 864E5 * c), j = "; expires=" + j.toGMTString());
				f && (i = "; secure");
				document.cookie = a + "=" + encodeURIComponent(b) + j + "; path=/" + k + i
			},
			remove:function(a, b){
				this.set(a, "", -1, b)
			}
		},

		getCookie:function(a){
			var cookie=this.cookie.get(a);
			if(cookie){
				return cookie
			}else{
				if(window.localStorage){
					var b=localStorage.getItem(a);
					b && this.cookie.set(a,b,365);
					return b
				}else{
					return null
				}
			}
		},

		setCookie:function(a,b,c){
			this.cookie.set(a,b,c);
			window.localStorage && localStorage.setItem(a,b)
		},

		removeCookie:function(a){
			this.cookie.remove(a);
			window.localStorage && localStorage.removeItem(a)
		},

		defaultId:function() {
			return Math.random().toString(36).substr(2,9)
		},

		validate:function(){
			if(typeof this.trackingId!='string'){
				console.log('trackingid is required!');
				return false
			}
			return true
		}
	}

	var parllayTracking=new tracking();
	
	window.fht=function(){
		switch(arguments[0]){
			case 'create':
			default:
				parllayTracking.trackingId=arguments[1];
				parllayTracking.subDomain=arguments[2];
				parllayTracking.callback=arguments[3]
		}
	}

	window.fa=function(){
		if(parllayTracking.validate()){
			switch(arguments[0]){
				case 'add':
					parllayTracking.setIdentifyUser(arguments[1]);
					break;
				case 'regist':
					parllayTracking.setRegistUser(arguments[1]);
					break;
				case 'reset':
					parllayTracking.removeRegistUser();
					break;
				case 'send':
				default:
					parllayTracking.track(arguments[1],arguments[2],arguments[3])
			}
		}
	}

})(window)